package com.hiray.demos

import android.content.Intent
import android.os.Bundle
import android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.hiray.demos.android_features.AndroidFeatureActivity
import com.hiray.demos.behavior.BehaviorEntryActivity
import com.hiray.demos.entry.*
import com.hiray.demos.etc.LargeTextRoomActivity
import com.hiray.demos.rv_related.DiffUtilActivity
import com.hiray.demos.rv_related.LayoutManagerEntryActivity
import com.hiray.demos.ui_architect.litho.LithoIndexActivity
import com.hiray.demos.visualEffect.ScrollMultiSelectActivity
import com.hiray.demos.visualEffect.SearchPrevActivity
import com.hiray.demos.visualEffect.SlideBackActivity
import com.hiray.demos.visualEffect.ViewActivity
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import kotlinx.android.synthetic.main.activity_main.*
import me.ele.uetool.UETool
import me.ele.uetool.Util


class MainActivity : AppCompatActivity(), ItemAdapter.OnItemClickListener {

    lateinit var toast: Toast
    val items = arrayListOf(
            "SurfaceView 大转盘",//0
            "萤火虫视频桌面",//1
            "FingerPrint(指纹解锁)",//2
            "浏览器唤醒App",//3
            "ScrollingActivity",//4
            "View跨页面转场过渡效果",//5
            "RV-Related",//6
            "AccessibilityService",//7
            "DiffUtil",//8
            "饿了么 搜索框效果",//9
            "RandomTextView",//10
            "Android Features",//11
            "即刻App侧滑返回效果",//12
            "Behavior",//13
            "Room数据库",//14
            "滑动多选效果(实现OnItemTouchListener)",//15
            "Litho 构建页面"
    )


    override fun onItemClick(position: Int) {
        overridePendingTransition(0, 0)
        when (position) {
            0 -> quickStartActivity(SurfaceViewActivity::class.java)
            1 -> quickStartActivity(VideoWallPaperActivity::class.java)
            2 -> quickStartActivity(FingerPrintUnlockActivity::class.java)
            3 -> quickStartActivity(BrowserLaunchAppActivity::class.java)
            4 -> quickStartActivity(ScrollingActivity::class.java)
            5 -> quickStartActivity(CrossActivityShareTransitionActivity::class.java)
            6 -> quickStartActivity(LayoutManagerEntryActivity::class.java)
            7 -> {
                val intent = Intent(ACTION_ACCESSIBILITY_SETTINGS)
                startActivity(intent)
            }
            8 -> quickStartActivity(DiffUtilActivity::class.java)
            9 -> quickStartActivity(SearchPrevActivity::class.java)
            10 -> quickStartActivity(ViewActivity::class.java)
            11 -> quickStartActivity(AndroidFeatureActivity::class.java)
            12 -> quickStartActivity(SlideBackActivity::class.java)
            13 -> quickStartActivity(BehaviorEntryActivity::class.java)
            14 -> quickStartActivity(LargeTextRoomActivity::class.java)
            15 -> quickStartActivity(ScrollMultiSelectActivity::class.java)
            16 -> quickStartActivity(LithoIndexActivity::class.java)

        }
    }

    @SuppressWarnings("showToast")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val quitAppMsg = resources.getString(R.string.confirm_quit_app)
        toast = Toast.makeText(this@MainActivity, quitAppMsg, Toast.LENGTH_SHORT)
        val itemAdapter = ItemAdapter(items, this)
        itemAdapter.cornerMarker = true
        recyclerview.adapter = itemAdapter
        recyclerview.addItemDecoration(HorizontalDividerItemDecoration.Builder(this@MainActivity)
                .colorResId(R.color.divider_lighter)
                .size(2)
                .build())
        // Example of a call to a native method
//        sample_text.text = stringFromJNI()
    }

    override fun onBackPressed() {
        if (toast.view.parent == null) {
            toast.show()
        } else System.exit(0)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main_menu, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.ue_tool -> UETool.showUETMenu()
        }
        return super.onOptionsItemSelected(item)
    }
}
